#pragma once

#include <string>
#include <string.h>
#include <vector>
#include <QRecursiveMutex>

namespace qb{
	namespace ht{

		typedef int HTUserId;
#define ROUND4(num) ((num%4==0)?num:((num/4+1)*4))
#define ROUND_ALIGN(NUM, SIZE) ((NUM%SIZE==0)?NUM:((NUM/SIZE+1)*SIZE))

		class FastLock{
			QRecursiveMutex	m_cs;
		public:
            FastLock(){}
            ~FastLock(){}
            bool Lock(){ m_cs.lock(); return true; }
            bool Unlock(){ m_cs.unlock(); return true; }
		};
		class ScopedLock{
			FastLock	*mx;
		public:
			ScopedLock(FastLock& lock) :mx(&lock){ mx->Lock(); }
			~ScopedLock(){ mx->Unlock(); }
		};
		struct Range{
			int		start, end;
		};
		typedef int RecordId;		// 表示File中的记录编号
		typedef int RowId;			// 表示表格中显示的行号0-N

		class BitFlag{// 性能不如vector<bool>
			enum{
				FLAG_WIDE = 8,
				FLAG_EXP = 3
			};
			unsigned short		m_capcity;
			unsigned short		m_count;
			bool*				m_flags;
		public:
			BitFlag(int capcity = 32) :m_capcity(capcity), m_count(0){
				m_flags = new bool[m_capcity];
				memset(m_flags, 0, m_capcity);
			}
			~BitFlag(){ if (m_flags){ delete[] m_flags; } }
			void clear(){ m_count = 0; }
			void push_back(bool flag){
				if (m_count >= m_capcity){
					int capcity = m_capcity * 2;
					bool* flags = new bool[capcity];
					memset(flags, 0, capcity);
					memcpy(flags, m_flags, m_capcity);
					delete[] m_flags;
					m_capcity = capcity;
					m_flags = flags;
				}
				m_flags[m_count++] = flag;
			}
			int size()const{ return m_count; }
			bool operator[](int index)const{ return m_flags[index]; }
			bool at(int index)const{ return m_flags[index]; }
			bool& at(int index){ return m_flags[index]; }
		};
        typedef std::vector<bool> BoolVec;

		typedef unsigned int FontId;


		class BondKeyGenerator{	// 用于产生随机的BondKey数据
			/*
			BondKey 由四部分组成
			1:A-Z,DSC,SH
			2:10个数字字符构成，前6个表示整数序号，后四个表示年份
			3:“ABSAMP,ABSCBS,ABSLBS,ABSMBS,CBB,CBBSCB,CORCCB,CORCCP,CORCEB,CORCMN,CORCOB,CORCSP,CORCVB,CORLCP,CORLEB,CORLMN,CORLSP,CORPPN,CORRAB,CORSCB,CORSCV,FINCBB,FINCSB,FINCXB,FINNCB,FINPBB,FINPSB,FINSEB,FINSES,FINSSB,GBBHJB,GBBRAB,GOVBGB,INT,LGBLLB,LGBTLB,NCD”
			4:2-4个数字字符构成
			*/
			typedef std::vector<std::string> stringvec;
			stringvec	m_part1;
			stringvec	m_part3;
		public:
			BondKeyGenerator();
			static BondKeyGenerator& Get();
			int Make(char* buf, int size);

		};

		/*struct tBondKey{
			unsigned short	cap;
			unsigned short	len;
			char			str[32];
		};*/
		//struct xPriceUnitc{
		//	//tBondKey	m_bondkey;
		//	char	m_bondkey[32];
		//	int		m_padding;
		//	float	m_float;
		//	double	m_double;
		//	int		m_int;
		//	char	m_string[32];
		//	time_t	m_time;
		//	xPriceUnitc(){ memset(this, 0, sizeof(*this)); }
		//};
	}//namespace ht
}//namespace qb
